<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
        public function __construct()
    {
        parent::__construct();
            // Your own constructor code
      $this->load->library('MyCategories'); 
      
       

       
    } 
    public function get_adres_tipi()
    {
        
        //die();
            if((int)$this->input->post('adrestipi') === 1)
            {
                $this->session->set_userdata('adrestipi1',true);
                $this->session->set_userdata('adrestipi2',false);
            }
            if((int)$this->input->post('adrestipi') === 2)
            {
                $this->session->set_userdata('adrestipi2',true);
                $this->session->set_userdata('adrestipi1',false);
            }
            $this->user_details();
    } 
    public function user_details()
    {
        $user_id = $this->session->userdata('user_id');
        if($this->user_model->get_user_information($user_id))
        {
            $data['user_information'] = $this->user_model->get_user_information($user_id);
            // dier stepe yonlendir 
            $this->payment();
        }
        else
        {
         


            
            
            if($this->session->userdata('adrestipi1'))
            {
                 
               /* $data['subview'] = 'checkout_part2_sahis';
    	        $this->load->view('layouts/default1',$data);*/
                $this->user_details_sahis();
            }
            if($this->session->userdata('adrestipi2'))
            {
                
             /*   $data['subview'] = 'checkout_part2_tuzel';
    	        $this->load->view('layouts/default1',$data); */
                  $this->user_details_tuzel();
            }
            $adrestipi = $this->session->userdata('adrestipi2');
            if(!$adrestipi)
            {
             
               
                  $this->user_details_sahis();
              /*  $data['subview'] = 'checkout_part2_sahis';
    	        $this->load->view('layouts/default1',$data);*/
            }
           

        }
     
    }
    public function user_details_sahis()
    {
        
            $this->form_validation->set_rules('detail_type', 'Kullanıcı tipi', 'required|xss_clean');
            $this->form_validation->set_rules('vergino', 'Vergi No', 'required|xss_clean|numeric|exact_length[10]');
            $this->form_validation->set_rules('tcno', 'TC Kimlik No', 'required|xss_clean|numeric|exact_length[11]');
            $this->form_validation->set_rules('vergidno','Vergi daiersi', 'required|xss_clean');
            $this->form_validation->set_rules('vergiil','Vergi İl', 'required|xss_clean');
            $this->form_validation->set_rules('adres','Adres', 'required|xss_clean');
            $this->form_validation->set_rules('evtel');
            $this->form_validation->set_rules('ceptel');
            
        /*    echo "<pre>";
            var_dump($this->input->post());
            echo "</pre>";
            die(); */
            
      		if ($this->form_validation->run() == FALSE)
    		{
    		    $data['subview'] = 'checkout_part2_sahis';
    	        $this->load->view('layouts/default1',$data);
    		}
    		else
    		{
                 $sahis_info = array(
                    'user_id' => $this->session->userdata('user_id'),
                    'tcno'  =>  $this->input->post('tcno'),
                    'vergino'   =>  $this->input->post('vergino'),
                    'vergidno'  =>  $this->input->post('vergidno'),
                    'vergiil'   =>  $this->input->post('vergiil'),
                    'adres' =>  $this->input->post('adres'),
                    'evtel' =>  $this->input->post('evtel'),
                    'ceptel'    =>  $this->input->post('ceptel'),
                    'dateadd'   => date("Y-m-d H:i:s"),
                    'dateupd'   =>  date("Y-m-d H:i:s")
                 );
                 $this->db->insert('user_sahis',$sahis_info);
                 $this->db->where('user_id', $this->session->userdata('user_id'));
                 $this->db->update('users', array('detail_type' => 1)); 
                
                 $this->payment();
            } 
        
    }
    public function user_details_tuzel()
    {
     
          $this->form_validation->set_rules('detail_type', 'Kullanıcı tipi', 'required|xss_clean');
            $this->form_validation->set_rules('vergino', 'Vergi No', 'required|xss_clean|numeric|exact_length[10]');
            
            $this->form_validation->set_rules('vergidno','Vergi daiersi', 'required|xss_clean');
            $this->form_validation->set_rules('vergiil','Vergi İl', 'required|xss_clean');
            $this->form_validation->set_rules('adres','Adres', 'required|xss_clean');
            $this->form_validation->set_rules('telefon','Telefon','required|xss_clean');
            $this->form_validation->set_rules('email', 'Email','required|valid_email|xss_clean');
            
        /*    echo "<pre>";
            var_dump($this->input->post());
            echo "</pre>";
            die(); */
            
      		if ($this->form_validation->run() == FALSE)
    		{
    		  	
    		    $data['subview'] = 'checkout_part2_tuzel';
    	        $this->load->view('layouts/default1',$data);
    		}
    		else
    		{
    		
                 $sahis_info = array(
                    'user_id' => $this->session->userdata('user_id'),
                    
                    'vergino'   =>  $this->input->post('vergino'),
                    'vergidno'  =>  $this->input->post('vergidno'),
                    'vergiil'   =>  $this->input->post('vergiil'),
                    'adres' =>  $this->input->post('adres'),
                    'telefon' =>  $this->input->post('telefon'),
                    'email'    =>  $this->input->post('email'),
                    'dateadd'   => date("Y-m-d H:i:s"),
                    'dateupd'   =>  date("Y-m-d H:i:s")
                 );
                 $this->db->insert('user_tuzel',$sahis_info);

                 $this->db->where('user_id', $this->session->userdata('user_id'));
                 $this->db->update('users', array('detail_type' => 2)); 
                 $this->payment();
            }         
    }
    public function payment()
    {
        $user_id = $this->session->userdata('user_id');
        $data['user_information'] = $this->user_model->get_user_information($user_id)->row(0);
        $data['adres_tipi'] = $this->db->where('user_id',$user_id)->get('users')->row(0)->detail_type;
        $data['subview'] = 'payment';
        $this->load->view('layouts/default1',$data);
    }
    public function checkout()
    {
        // once bak loginmi deilse login et
       
        if($this->session->userdata('logged_in'))
        {
            $this->user_details();
        }
        else
        {
            
            
             $this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'required|xss_clean|min_length[5]');
            
      		if ($this->form_validation->run() == FALSE)
    		{
    		    $data['subview'] = 'view_login3';
    	        $this->load->view('layouts/default1',$data);
    		}
    		else
    		{
                 // Get User Paramaters
    		     $email = $this->input->post('email');
                 $password = $this->input->post('password'); 
                 
                 // Use the User model
    			 
                 
                 $user_id = $this->user_model->check_login($email,$password);
                 
                
                 if(!$user_id)
                 {
                   
                    $this->session->set_flashdata('errormsg', "Kullanıcı adı veya şifre hatalı girildi.");
                    redirect('cart/checkout');
                 }
                 else 
                 {
                    $user_info = $this->user_model->get_user_details($user_id); 
                    if((int)$user_info->row(0)->status === PASSIVE)
                    {
                         $this->session->set_flashdata('alertmsg', "Hesabınız aktif edilmemiştir lütfen size gönderilen email ile aktifleştirin.");
                         redirect('cart/checkout');
                    }
                    else
                    {
                        if($this->db->where(array('cisession'=>$this->session->userdata['session_id'],'status' => 1))->get('cart')->num_rows()>0)
                        {
                            $cart_id = $this->db->where(array('cisession'=>$this->session->userdata['session_id'],'status' => 1))->get('cart')->row(0)->cart_id;
                            $this->db->where('cart_id',$cart_id);
                            $this->db->update('cart',array('customer_id'=>$user_info->row(0)->user_id));
                        }
                    	$this->session->set_userdata(array(
        								'logged_in' =>  true, 
        								'user_id'   =>  $user_info->row(0)->user_id,
                                        'email'     =>  $user_info->row(0)->email,
                                        'type'      =>  $user_info->row(0)->type
        								));
        
        				$this->user_details();                    
                        
                    }            				
    
                    
                 }
    		  }  
        }

      
        
      
      
    }
    public function test()
    {
        echo "<pre>";
        print_r($this->session->userdata);
    }
    
    public function sepetim()
    {
        $data['subview'] = 'sepetim';
        $this->load->view('layouts/default_cart',$data);
    }

    public function remove_product($cart_id,$product_id)
    {
        
        $this->db->delete('cart_products', array('cart_id' => $cart_id, 'product_id' => $product_id));
         $this->session->set_flashdata('infomsg','Ürününüz silindi');
         $ref = $this->input->server('HTTP_REFERER', TRUE);
         redirect($ref, 'location'); 
        
    }
	public function index()
	{
        $this->load->view('layouts/dashboard');
	}
    public function update_cart()
    {
      
                   $update_data = array(
                               'quantitiy' =>  $this->input->post('adet')
                            );
                
                $this->db->where(array('cart_id' => $this->session->userdata['cart_id'] , 'product_id'=> $this->input->post('product_id')));
                $this->db->update('cart_products', $update_data); 
                            $ref = $this->input->server('HTTP_REFERER', TRUE);
redirect($ref, 'location'); 
        
    }
    public function add_to_cart()
    {
    /*    echo "<pre>";
        var_dump($this->input->post());
        die(); */
        
                if($this->input->post('size_id'))
            {
                $size = $this->input->post('size_id');
            }
            else
            {
                $size = -1;
            }
            if($this->input->post('color_id'))
            {
                $color = $this->input->post('color_id');
            }
            else
            {
                $color = -1;
            } 
        
        /* eger aynı ürün ekleniosa quantitiyi update et  toplama islemleri ona baglı */
        
        
        // eger loginse customer_id den bak statusu active olan cart_id varsa ona ekle
        // login deilse sessiona bak urun varsa ona ekle
        // hiç biri yoksa
            //bak loginse customer id ile new cart_id ekle
            //login deilse session ile cart_id ekle
        
        // eger loginse customer_id den bak statusu active olan cart_id varsa ona ekle
        if($this->session->userdata('logged_in'))
        {
            $customer_id = $this->session->userdata('user_id');
               
            if($this->db->where(array('customer_id' => $customer_id , 'status' => 1))->get('cart')->num_rows()>0)
            {
                $cart_id = $this->db->where(array('customer_id' => $customer_id , 'status' => 1))->get('cart')->row(0)->cart_id;
              if($this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->num_rows()>0)
              {
                                $qun =$this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->row(0)->quantitiy;
                                $update_data = array(
                               'quantitiy' => $qun+ $this->input->post('adet')
                            );
                
                $this->db->where(array('cart_id' => $cart_id , 'product_id'=>$this->input->post('product_id')));
                $this->db->update('cart_products', $update_data); 
                
              }
              else
              {             
                $this->db->insert('cart_products',
                array('cart_id'=>$cart_id,
                'product_id' =>$this->input->post('product_id'),
                'quantitiy'=>$this->input->post('adet'),
                'seller_id' => 0,
                'attributes' =>3,
                'module' => 5,
                'size_id'   => $size,
                'color_id'  =>  $color
                ));
                
              }

            }
            else
            {
                $time = date("Y-m-d H:i:s");
                $this->db->insert('cart',array('customer_id' => $customer_id, 'delivery_option' => 'test', 'date_upd' => $time, 'date_add' => $time));
                $cart_id = $this->db->where(array('customer_id' => $customer_id , 'status' => 1))->get('cart')->row(0)->cart_id;
                
              if($this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->num_rows()>0)
              {
                                $qun =$this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->row(0)->quantitiy;
                                $update_data = array(
                               'quantitiy' => $qun+$this->input->post('adet')
                            );
                
                $this->db->where(array('cart_id' => $cart_id , 'product_id'=>$this->input->post('product_id')));
                $this->db->update('cart_products', $update_data); 
                
              }
              else
              {
                                $this->db->insert('cart_products',
                array('cart_id'=>$cart_id,
                'product_id' =>$this->input->post('product_id'),
                'quantitiy'=>$this->input->post('adet'),
                'seller_id' => 0,
                'attributes' =>3,
                'module' => 5,
                'size_id'   => $size,
                'color_id'  =>  $color
                ));
              }
                

                
            }

        }
        else
        {
            // login deilse sessiona bak urun varsa ona ekle
            $session_id = $this->session->userdata['session_id'];
            $time = date("Y-m-d H:i:s");
            if($this->db->where(array('cisession' => $session_id,'status' => 1))->get('cart')->num_rows()>0)
            {
                // session hala acık ver sepet olusturulmus zaten
                
                 $cart_id = $this->db->where(array('cisession' => $session_id , 'status' => 1))->get('cart')->row(0)->cart_id;
                 
            if($this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->num_rows()>0)
              {
                                $qun =$this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->row(0)->quantitiy;
                                $update_data = array(
                               'quantitiy' => $qun+$this->input->post('adet')
                            );
                
                $this->db->where(array('cart_id' => $cart_id , 'product_id'=>$this->input->post('product_id')));
                $this->db->update('cart_products', $update_data); 
                
              }
              else
              {
                                $this->db->insert('cart_products',
                array('cart_id'=>$cart_id,
                'product_id' =>$this->input->post('product_id'),
                'quantitiy'=>$this->input->post('adet'),
                'seller_id' => 0,
                'attributes' =>3,
                'module' => 5,
                'size_id'   => $size,
                'color_id'  =>  $color
                ));
              }

               
            }
             // hiç biri yoksa
            else
            {
                 $this->db->insert('cart',array('cisession' => $session_id, 'delivery_option' => 'test', 'date_upd' => $time, 'date_add' => $time));
                $cart_id = $this->db->where(array('cisession' => $session_id , 'status' => 1))->get('cart')->row(0)->cart_id;
                
              if($this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->num_rows()>0)
              {
                                $qun =$this->db->where(array('cart_id' => $cart_id, 'product_id' => $this->input->post('product_id')))->get('cart_products')->row(0)->quantitiy;
                                $update_data = array(
                               'quantitiy' => $qun+1
                            );
                
                $this->db->where(array('cart_id' => $cart_id , 'product_id'=>$this->input->post('product_id')));
                $this->db->update('cart_products', $update_data); 
                
              }
              else
              {
                                
                $this->db->insert('cart_products',
                array('cart_id'=>$cart_id,
                'product_id' =>$this->input->post('product_id'),
                'quantitiy'=>$this->input->post('adet'),
                'seller_id' => 0,
                'attributes' =>3,
                'module' => 5,
                'size_id'   => $size,
                'color_id'  =>  $color
                ));
              }

            }
        }
        if($this->input->post('hemen_al') === "")
        {
             redirect('checkout', 'refresh');
        }
        else
        {
                        $this->session->set_flashdata('successmsg','Ürününüz eklendi');
                $ref = $this->input->server('HTTP_REFERER', TRUE);
    redirect($ref, 'location'); 
        }

    }  
         
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */